package vn.aptech.scrumgroup.model;

import vn.aptech.scrumgroup.event.BrilliantEvent;
import vn.aptech.scrumgroup.event.MoveRoundEvent;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
import demo.axitdn.physic.Vector3f;

public class TextOnArcModel extends AbstractOnCircle {

	private CircleWithTextModel innerCircle, outerCircle;
	private float cx, cy, radius;
	private float charWidth;
	private float startAngle;
	private String text;
	private boolean isBrilliant = false;
	float beta;

//	public TextOnArcModel() {
//		// TODO Auto-generated constructor stub
//		paint = new Paint();
//	}

	public TextOnArcModel(CircleWithTextModel innerCircle, CircleWithTextModel outerCircle,
			float charWidth, float startAngle, String text, int foreColor) {
		super(-1, foreColor);
		setProperties(innerCircle.getCx(), innerCircle.getCy(),
				innerCircle.getRadius(), charWidth, startAngle,
				text);
		this.innerCircle = innerCircle;
		this.outerCircle = outerCircle;
	}//end constructor

	public void setProperties(float cx, float cy, float radius,
			float charWidth, float startAngle, String text) {
		this.cx = cx;
		this.cy = cy;
		this.radius = radius;
		this.charWidth = charWidth;
		this.startAngle = startAngle;
		this.text = text;
	}

	public float getCx() {
		return cx;
	}

	public void setCx(float cx) {
		this.cx = cx;
	}

	public float getCy() {
		return cy;
	}

	public void setCy(float cy) {
		this.cy = cy;
	}

	public float getRadius() {
		return radius;
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public float getCharWidth() {
		return charWidth;
	}

	public void setCharWidth(float charWidth) {
		this.charWidth = charWidth;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setBrilliant(boolean b) {
		
	}// end set

	public boolean getBrilliant() {
		return isBrilliant;
	}

	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		ColorMatrix cm = new ColorMatrix();
		int brightness = 120;
		if (isBrilliant) {
			cm.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0,
					0, 1, 0, brightness, 0, 0, 0, 1, 0 });
			paint.setColorFilter(new ColorMatrixColorFilter(cm));
		} else{
			paint.setColorFilter(null);
		}
		paint.setColor(foreColor);
		drawTextOnCircleOutline(c, text, cx, cy, radius, paint, charWidth,
				startAngle);
		beta = paint.measureText(text) / radius;
	}

	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub

	}

	private void drawTextOnCircleOutline(Canvas canvas, String text, float x,
			float y, float radius, Paint paint, float charWidth,
			float startAngle) {
		if (charWidth > 0) {
			paint.setStrokeWidth(charWidth);
			paint.setFakeBoldText(false);
			paint.setTextSize((outerCircle.getRadius()-innerCircle.getRadius())/2);
			Rect rect = new Rect();
			paint.getTextBounds(text, 0, text.length(), rect);
			radius = (outerCircle.getRadius()+innerCircle.getRadius()-rect.height())/2;
		}//end if
		paint.setStyle(Style.STROKE);
		// create path to draw text on it
		Path path = new Path();
		path.addCircle(x, y, radius, Path.Direction.CW);
		if (startAngle < 0)
			startAngle = (float) (2 * Math.PI + startAngle);
		float startPosition = (float) (startAngle * radius - paint
				.measureText(text) / 2);
		if (startPosition < 0)
			startPosition += 2 * Math.PI * radius;
		startPosition = (float) (startPosition % (2 * Math.PI * radius));
		if (startPosition + paint.measureText(text) > 2 * Math.PI * radius) {
			float cut = (float) (2 * Math.PI * radius - startPosition);
			int index = 0;
			while (paint.measureText(text, 0, index) <= cut) {
				index++;
			}
			String aboveStr = text.substring(0, index - 1);
			String belowStr = text.substring(index - 1);
			canvas.drawTextOnPath(aboveStr, path,
					startPosition + cut - paint.measureText(aboveStr), 0, paint);
			canvas.drawTextOnPath(belowStr, path, 0, 0, paint);
		} else
			canvas.drawTextOnPath(text, path, startPosition, 0, paint);
	}//end draw

	@Override
	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub
		double d = Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));

		Vector3f vM = new Vector3f(x, y, 0);
		Vector3f vCM = vM.subtract(new Vector3f(cx, cy, 0));

		float alpha = vCM.angleBetween(Vector3f.UNIT_X);
		if (y - cy < 0)
			alpha = -alpha;
//		float beta = paint.measureText(text) / radius;

		float startAngle = (float) (this.startAngle % (2 * Math.PI));
		if (startAngle > Math.PI)
			startAngle = (float) (startAngle - 2 * Math.PI);
		startAngle -= beta / 2;

		if (innerCircle.getRadius() < d && d < outerCircle.getRadius()) {
			if (startAngle < alpha && (alpha - startAngle) < beta)
				return true;
		}
		return false;
	}// end isPointOf

//	@Override
//	public boolean onPressed(int x, int y) {
//		// TODO Auto-generated method stub
//		isPressed = true;
//		setBrilliant(true);
//		fireTurnOffLight(new BrilliantEvent(50));
//		return super.onPressed(x, y);
//	}// end onPressed
//
//	@Override
//	public boolean onReleased(int x, int y) {
//		// TODO Auto-generated method stub
//		isPressed = false;
//		setBrilliant(false);
//		return super.onReleased(x, y);
//	}

	public void setCircle(CircleWithTextModel model) {
		// TODO Auto-generated method stub
		this.innerCircle = model;
	}

	public CircleWithTextModel getCircle() {
		// TODO Auto-generated method stub
		return innerCircle;
	}

	public void moveRoundOnCircle(float angle) {
		// TODO Auto-generated method stub
		float alpha = this.startAngle + angle;
		if (alpha > Math.PI)
			alpha = (float) (alpha - 2 * Math.PI);
		if (alpha < -Math.PI)
			alpha = (float) (alpha + 2 * Math.PI);
		this.startAngle = alpha;
	}

	public void actionPerformed(BrilliantEvent evt) {
		// TODO Auto-generated method stub
	}// end actionPerformed

	public void actionPerformed(MoveRoundEvent evt) {
		// TODO Auto-generated method stub
		moveRoundOnCircle(evt.getAngle());
	}

	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub
		
	}

}
